#include "server.h"
#include <QtDebug>

Server::Server(int listenPort)
{
    this->port = listenPort;
    this->tcpServer = new QTcpServer();
    this->tcpServer->listen(QHostAddress::Any, listenPort);

    qDebug() << "Server is listening on port: " << this->tcpServer->serverPort();

    this->connect(this->tcpServer, SIGNAL(newConnection()), this, SLOT(HandleNewConnection()));


}

void Server::HandleNewConnection()
{
    QTcpSocket* clientSocket = this->tcpServer->nextPendingConnection();
    this->Respond(*clientSocket);

}


void Server::Respond(QTcpSocket &clientSocket)
{


    qDebug() << "Getting ready to respond to:" << clientSocket.peerAddress().toString()
            << "on port:" << clientSocket.peerPort();


    //QByteArray barray;
    //QDataStream ds = new QDataStream(&barray, QIODevice::WriteOnly);
    ////As per docs, this is a moving target and
    //ds.setVersion(QDataStream::Qt_4_4);

    clientSocket.write("Hi mister client");


}


